/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clasesbasicas;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import usecase.IStep;
import usecase.UseCaseIteratorFactory;
import usecase.UseCaseXML;

/**
 *
 * @author laura
 */
public class Manejador {
    
     /** Stores IControlFlow objects */
    private ArrayList<Paso> listaTotalPasos ;
    
     /** 
     * Creates a new instance of ActivityDiagramGenerator 
     */
    public Manejador () {
        this.listaTotalPasos = new ArrayList<Paso>();
        
    }
    
    
     /** Generate an activity diagram from a use case */
    public void generate (UseCaseXML uc) {
        Iterator ms;
        IStep mainS;
        
        
        ms = uc.iteratorMainSequence ();
        while (ms.hasNext ()) {
            mainS = (IStep) ms.next ();
            //this.generatePreCond (uc, mainS);
            //this.generatePostCond (uc, mainS);
            this.generatePostCondMethod(uc, mainS);
            this.generatePreCondMethod(uc, mainS);
        }        
    }
    
     /** Helpper method
     * Traverse the precondition steps and calls cretaePreCond to build
     * a decision node for each precondition step.
     **/
    private void generatePreCond (UseCaseXML uc, IStep s) {
        IStep pre;
        // Obtenemos el iterador adecuado segun el tipo de plantillA
        //Iterator it = uc.iteratorPreSequence (s);
        Iterator it;
        it = UseCaseIteratorFactory.GetIteratorPreevaluatedSteps (uc, s);       
        
        while(it.hasNext ()) {
            pre = (IStep) it.next ();
            //createPreCond (pre);
            
            //System.out.println("Condicion: "+pre.getCondition());
            //System.out.println("Action: "+pre.getAction());
            
            Paso paso = new Paso();
            paso.setCausa(pre.getCondition());           
            paso.setEfecto(pre.getAction());
            
            getListaTotalPasos().add(paso);
        }
        
        
    }
    
    /** Helpper method
     **/
    private void generatePostCond (UseCaseXML uc, IStep s) {
        IStep pos;
        // Obtenemos el iterador adecuado segun el tipo de plantilla
        //Iterator it = uc.iteratorInvSequence (s);
        Iterator it;
        it = UseCaseIteratorFactory.GetIteratorPostevaluatedSteps (uc, s);
       
        
        while(it.hasNext ()) {
            pos = (IStep) it.next ();
            Paso paso = new Paso();
            paso.setCausa(pos.getCondition());           
            paso.setEfecto(pos.getAction());
            
            getListaTotalPasos().add(paso);
           
        }
    }
    
    /** Helpper method
     **/
    private void generatePostCondMethod (UseCaseXML uc, IStep s) {
        IStep pos;
        // Obtenemos el iterador adecuado segun el tipo de plantilla
        //Iterator it = uc.iteratorInvSequence (s);
        Iterator it;
        it = UseCaseIteratorFactory.GetIteratorPostevaluatedStepsMethod (uc, s);       
        
        while(it.hasNext ()) {
            pos = (IStep) it.next ();
            Paso paso = new Paso();
            paso.setCausa(pos.getCondition());           
            paso.setEfecto(pos.getAction());            
            getListaTotalPasos().add(paso);
           
        }
    }
    
     /** Helpper method
     * Traverse the precondition method steps 
     **/
    private void generatePreCondMethod (UseCaseXML uc, IStep s) {
        IStep pre;
        // Obtenemos el iterador adecuado segun el tipo de plantillA
        //Iterator it = uc.iteratorPreSequence (s);
        Iterator it;
        it = UseCaseIteratorFactory.GetIteratorPreevaluatedStepsMethod (uc, s);         
        
        while(it.hasNext ()) {
            pre = (IStep) it.next ();                
            Paso paso = new Paso();
            paso.setCausa(pre.getCondition());           
            paso.setEfecto(pre.getAction());            
            getListaTotalPasos().add(paso);
        }
    }
    
    


    /**
     * @return the listaTotalPasos
     */
    public ArrayList<Paso> getListaTotalPasos() {
        return listaTotalPasos;
    }

    /**
     * @param listaTotalPasos the listaTotalPasos to set
     */
    public void setListaTotalPasos(ArrayList<Paso> listaTotalPasos) {
        this.listaTotalPasos = listaTotalPasos;
    }
    
    
    
    
    
}
